package com.lyn.auxiliarytool.contract;

import java.util.List;

/**
 * @author Lyn 2022/11/29
 * @version 1.0.0
 */
public class Result {

    public final static int SUCCESS = 1;

    public final static int FAIL = 0;


    private int code;

    private String msg;

    private Object data;

    private String callMethodName;


    public Result(int code, String msg, Object data, String callMethodName) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.callMethodName = callMethodName;
    }

    public static Result SUCCESS() {
        return new Result(SUCCESS, "SUCCESS", null, getMethodName());
    }

    public static Result SUCCESS(String msg) {
        return new Result(SUCCESS, msg, null, getMethodName());
    }

    public static <T> Result SUCCESS(T data) {
        return new Result(SUCCESS, "SUCCESS", data, getMethodName());
    }

    public static <T> Result SUCCESS(String msg, T data) {
        return new Result(SUCCESS, msg, data, getMethodName());
    }

    public static Result FAIL() {
        return new Result(FAIL, "FAIL", null, getMethodName());
    }

    public static Result FAIL(String msg) {
        return new Result(FAIL, msg, null, getMethodName());
    }

    public static <T> Result FAIL(T data) {
        return new Result(FAIL, "FAIL", data, getMethodName());
    }

    public static <T> Result FAIL(String msg, T data) {
        return new Result(FAIL, msg, data, getMethodName());
    }

    public static int getSUCCESS() {
        return SUCCESS;
    }

    public static int getFAIL() {
        return FAIL;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public <T> List<T> getListData(Class<T> tClass) {
        return (List<T>) data;
    }

    public <T> T getData(Class<T> tClass) {
        return (T) data;
    }

    public void setData(Object data) {
        this.data = data;
    }


    public String getCallMethodName() {
        return callMethodName;
    }


    public static String getMethodName() {
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        StackTraceElement stackTraceElement = stackTrace[2];
        return stackTraceElement.getMethodName();
    }
}

